const tokenKey = "Access-Token"
/**
 * 
 * @returns 获取token
 */
export const getAccessToken = (): string | null => {
    return getStroage().getItem(tokenKey);
}

/**
 * 获取bearer token 
 */
export function getBearerAccessToken() {
    var token = getAccessToken();
    return token ? "bearer " + token : null;
}
/**
 * 
 * @returns 获取token
 */
export const setAccessToken = (token: string) => {
    getStroage().setItem(tokenKey, token);
}

export const clearAccessToken = () => {
    getStroage().removeItem(tokenKey);
}


export const getStroage = (): Storage => {
    return sessionStorage;
}



/**
 * 将object对象转换为url参数
 * @param object 需要转换的对象
 * @param questionMark 是否需要?
 * @returns url
 */
export function objectToUrlParams(object: any, questionMark = false) {
    if (!object) return "";
    let url = "";
    Object.keys(object).forEach((key: string) => {
        if (object[key]) url += "&" + key + "=" + object[key]
    })
    if (!url) return "";
    return questionMark ? "?" + url.substring(1) : url;
}

/**
 * 生成UUid
 * @returns 返回32位uuid
 */
export function generateUUId() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

